﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;

namespace JobSeek
{
    public partial class login : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void BtnLogin_Click(object sender, EventArgs e)
        {
            using (job_seekerEntities ctx = new job_seekerEntities())
            {
                string username = tbxUsername.Text;
                string password = tbxPassword.Text;

                string hashPassword = FormsAuthentication.HashPasswordForStoringInConfigFile(password, "SHA1");
                var result = from user in ctx.Advertisers
                             where user.username == username
                             where user.password == hashPassword
                             select user;

                if (result.Count() == 1)
                {
                    //user will be logged in.
                    
                    FormsAuthentication.SetAuthCookie(username, false);

                    //check for user's role and redirect them accordingly

                   
                    if (Roles.IsUserInRole(username, "employer"))
                    {
                        Response.Redirect("#to employer page");
                    }
                }
                else
                {
                    //return wrong username or password message.
                    Response.Write("wrong username or password");
                }

            }
        }



    }
}